<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="app包名" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入app包名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wxconfig:param:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wxconfig:param:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wxconfig:param:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wxconfig:param:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paramList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="APP名称" align="center" prop="appName" />
      <el-table-column label="APP包名" align="center" prop="packageName" />
      <el-table-column label="appId" align="center" prop="appId" />
      <el-table-column label="appSecret" align="center" prop="appSecret" />
      <el-table-column label="商户号" align="center" prop="mchId" />
      <el-table-column label="v3密钥" align="center" prop="apiV3Key" />
      <el-table-column label="微信pem证书" align="center" prop="pem" />
      <el-table-column label="微信p12证书" align="center" prop="p12" />
      <el-table-column label="APP推广页面地址" align="center" prop="downloadUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wxconfig:param:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wxconfig:param:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="240px">
        <el-form-item label="app包名" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入app包名" />
        </el-form-item>
        <el-form-item label="appId" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入appId" />
        </el-form-item>
        <el-form-item label="appSecret" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="请输入appSecret" />
        </el-form-item>
        <el-form-item label="商户号" prop="mchId">
          <el-input v-model="form.mchId" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="v3密钥" prop="apiV3Key">
          <el-input v-model="form.apiV3Key" placeholder="请输入v3密钥" />
        </el-form-item>
        <el-form-item label="微信apiclient_key.pem证书" prop="pem">
<!--          <el-input v-model="form.pem" placeholder="请上传微信apiclient_key.pem证书" />-->
          <el-upload
            class="upload-demo"
            :headers="myHeaders"
            :action="uploadUrl"
            ref="pemUpload"
            :limit="1"
            accept=".pem"
            :on-exceed="handlePemExceed"
            :on-success="doAfterPemUpload"
            :show-file-list="false">
            <el-button size="small" type="primary">点击上传</el-button>
            <span>{{form.pem}}</span>
          </el-upload>
        </el-form-item>
        <el-form-item label="微信apiclient_cert.p12证书" prop="p12">
          <el-upload
            class="upload-demo"
            :headers="myHeaders"
            :action="uploadUrl"
            ref="p12Upload"
            :limit="1"
            accept=".p12"
            :on-exceed="handleP12Exceed"
            :on-success="doAfterP12Upload"
            :show-file-list="false">
            <el-button size="small" type="primary">点击上传</el-button>
            <span>{{form.p12}}</span>
          </el-upload>
<!--          <el-input v-model="form.p12" placeholder="请上传微信apiclient_cert.p12证书" />-->
        </el-form-item>
        <el-form-item label="APP推广页面地址" prop="downloadUrl">
          <el-input v-model="form.downloadUrl" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParam, getParam, delParam, addParam, updateParam } from "@/api/wxconfig/param";
import { getToken } from "@/utils/auth";

export default {
  name: "Param",
  data() {
    return {
      myHeaders: { Authorization: `Bearer ${getToken()}` },
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload2",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信参数配置表格数据
      paramList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        packageName: null,
        appId: null,
        appSecret: null,
        mchId: null,
        apiV3Key: null,
        pem: null,
        p12: null,
        notifyUrl: null,
        downloadUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        packageName: [
          { required: true, trigger: "blur", message: "请输入app包名" }
        ],
        appId: [
          { required: true, trigger: "blur", message: "请输入appId" }
        ],
        appSecret: [
          { required: true, trigger: "blur", message: "请输入appSecret" }
        ],
        mchId: [
          { required: true, trigger: "blur", message: "请输入商户号" }
        ],
        apiV3Key: [
          { required: true, trigger: "blur", message: "请输入v3密钥" }
        ],
        pem: [
          { required: true, trigger: "change", message: "请上传微信apiclient_key.pem证书" }
        ],
        p12: [
          { required: true, trigger: "change", message: "请上传微信apiclient_cert.p12证书" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信参数配置列表 */
    getList() {
      this.loading = true;
      listParam(this.queryParams).then(response => {
        this.paramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        packageName: null,
        appId: null,
        appSecret: null,
        mchId: null,
        apiV3Key: null,
        pem: null,
        p12: null,
        notifyUrl: null,
        downloadUrl: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信参数配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getParam(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信参数配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateParam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信参数配置编号为"' + ids + '"的数据项？').then(function() {
        return delParam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wxconfig/param/export', {
        ...this.queryParams
      }, `param_${new Date().getTime()}.xlsx`)
    },
    handleP12Exceed(files, fileList) {
      this.$modal.msgError(
        `当前限制选择 1 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    handlePemExceed(files, fileList) {
      this.$modal.msgError(
        `当前限制选择 1 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    doAfterP12Upload(resData, file) {
      if (resData.code == 200) {
        this.$modal.msgSuccess("上传成功");
        this.form.p12 = resData.fileName;
      } else {
        this.$modal.msgError(resData.msg);
      }
      this.$refs.p12Upload.clearFiles();
    },
    doAfterPemUpload(resData, file) {
      if (resData.code == 200) {
        this.$modal.msgSuccess("上传成功");
        this.form.pem = resData.fileName;
      } else {
        this.$modal.msgError(resData.msg);
      }
      this.$refs.pemUpload.clearFiles();
    },
  }
};
</script>
